<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.xsdacj_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>

</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/xsdacj";
  import {mapGetters} from "vuex";
  import {getStore} from "@/util/store";
  import {getBjTree, getXsTree} from "@/api/service/org";

  export default {
    data() {
      return {
        treeBj:'0',
        treeData: [],
        treeOption: {
          nodeKey: 'id',
          lazy: false,
          addBtn: false,
          menu: false,
          size: 'small',
          defaultExpandAll:true,
          props: {
            labelText: '标题',
            label: 'title',
            value: 'value',
            children: 'children'
          }
        },
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          menuType:'menu',
          menuBtnTitle:'更多操作',
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            {
              label: "学生",
              prop: "xsId",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入学生ID",
                trigger: "blur"
              }],
              type: "tree",
              search:false,
              dicData: [],
              props: {
                label: "title"
              },
              slot: true,
              checkStrictly: true,
              editDisabled:true,
            },
            {
              label: "学校",
              prop: "schoolId",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入学校名称",
                trigger: "blur"
              }],
              type: "select",
              dicUrl: "/api/school-service/school/select",
              props: {
                label: "xxName",
                value: "id"
              },
              editDisabled:true,
              addDisabled:true,
            },

            {
              label: "年级",
              prop: "njId",
              type: "select",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入年级名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/nj/select",
              props: {
                label: "njName",
                value: "id"
              },
              editDisabled:true,
              addDisabled:true,
            },
            {
              label: "班级",
              prop: "bjId",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入班级",
                trigger: "blur"
              }],
              type: "tree",
              dicData: [],
              props: {
                label: "title"
              },
              slot: true,
              checkStrictly: true,
              editDisabled:true,
              addDisabled:true,
            },
            {
              label: "学年",
              prop: "xnId",
              type: "select",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入年级名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/xninit/select",
              props: {
                label: "xnName",
                value: "id"
              },
              editDisabled:true,
              addDisabled:true,
            },
            {
              label: "学期",
              prop: "xqId",
              type: "select",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入年级名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/xq/select",
              props: {
                label: "xqName",
                value: "id"
              },
              editDisabled:true,
              addDisabled:true,
            },


            {
              label: "考试",
              prop: "kcExamId",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入考试ID",
                trigger: "blur"
              }],
              type: "select",
              dicUrl: "/api/school-service/examinit/select",
              props: {
                label: "examName",
                value: "id"
              },
              editDisabled:true,
            },
            {
              label: "课程",
              prop: "kcId",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入课程ID",
                trigger: "blur"
              }],
              type: "select",
              dicUrl: "/api/school-service/kcinit/select",
              props: {
                label: "kcName",
                value: "id"
              },
              editDisabled:true,
              addDisabled:true,
            },
            {
              label: "考试分数",
              prop: "kcExamScore",
              rules: [{
                required: true,
                message: "请输入考试分数",
                trigger: "blur"
              }]
            },
            {
              label: "考试编号",
              prop: "kcExamNo",
              overHidden:true,
              rules: [{
                required: false,
                message: "请输入考试编号",
                trigger: "blur"
              }]
            },

            {
              label: "考试时间",
              prop: "kcExamTime",
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              overHidden:true,
              rules: [{
                required: true,
                message: "请输入考试时间",
                trigger: "blur"
              }]
            },
            {
              label: "成绩附件",
              prop: "cjPic1",
              rules: [{
                required: true,
                message: "请输入任务附件1",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 24,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "其他附件1",
              prop: "cjPic2",
              rules: [{
                required: false,
                message: "请输入任务附件2",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
            {
              label: "其他附件2",
              prop: "cjPic3",
              rules: [{
                required: false,
                message: "请输入任务附件3",
                trigger: "blur"
              }],
              type: 'upload',
              loadText: '附件上传中，请稍等',
              limit:1,
              listType: 'picture-img',
              span: 8,
              propsHttp: {
                url:'link',
                name:'name',
                res: 'data'
              },
              action: '/api/kaibu-platform-service/oss/pri',
              tip: '只能上传jpg/png文件，且不超过500kb',
              hide: true
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.xsdacj_add, false),
          viewBtn: this.vaildData(this.permission.xsdacj_view, false),
          delBtn: this.vaildData(this.permission.xsdacj_delete, false),
          editBtn: this.vaildData(this.permission.xsdacj_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted() {

    },
    methods: {

      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;

        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });

        getXsTree(this.form.tenantId).then(res => {
          const column = this.findObject(this.option.column, "xsId");
          column.dicData = res.data.data;
        });

        getBjTree(this.form.tenantId).then(res => {
          const column = this.findObject(this.option.column, "bjId");
          column.dicData = res.data.data;
        });
        const school = this.findObject(this.option.column, "schoolId");
        school.hide = true;
      }
    }
  };
</script>

<style>
</style>
